<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="'CodeManagerPriv.Add'">
          新增
        </a-button>
      </template>

      <template #codeType="{ record }">
        <a @click="handleOpenListModal(record)">{{ record.codeType }}</a>
      </template>

      <template #fixed="{ record }">
        <CheckOutlined :style="{ color: '#67c23a' }" v-if="record.fixed" />
        <CloseOutlined :style="{ color: '#f56c6c' }" v-else />
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:bullet-list-line',
              onClick: handleOpenListModal.bind(null, record),
            },
            {
              auth: 'CodeManagerPriv.Edit',
              disabled: record.fixed,
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              auth: 'CodeManagerPriv.Delete',
              disabled: record.fixed,
              color: 'error',
              icon: 'ant-design:delete-outlined',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <CodeModal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getCodeList, delCode } from '/@/api/system/system';

  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import CodeModal from './CodeModal.vue';
  import { CheckOutlined, CloseOutlined } from '@ant-design/icons-vue';

  import { columns, searchFormSchema } from './code.data';
  import { useGo } from '/@/hooks/web/usePage';

  export default defineComponent({
    name: 'CodeManager',
    components: {
      BasicTable,
      TableAction,
      CodeModal,
      CheckOutlined,
      CloseOutlined,
    },
    setup() {
      const { createMessage } = useMessage();
      const go = useGo();
      const [registerTable, { reload }] = useTable({
        title: '代码列表',
        api: getCodeList,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleSuccess() {
        reload();
      }

      /***************************Modal***********************************/

      const [registerModal, { openModal }] = useModal();

      function handleCreate() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      async function handleDelete(record: Recordable) {
        await delCode(record.codeType);
        createMessage.success('删除成功');
        reload();
      }

      function handleOpenListModal(record: Recordable) {
        go('/system/code_list/' + record.codeType + '?allowAddItem=' + record.allowAddItem);
      }

      return {
        registerTable,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        registerModal,
        handleOpenListModal,
      };
    },
  });
</script>
